package com.yanqu.road.dao.impl.activity.langyabang;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.langyabang.data.LangYaBangWarReportData;
import com.yanqu.road.entity.activity.langyabang.data.LangYaUserBriefData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LangYaWarReportDaoImpl extends TempDataObjectDao<LangYaBangWarReportData> {
    public static String getLangYaBangWarReportDataSql = "select * from `t_u_lang_ya_war_report` where `activityId`=? and `userId`=? order by time desc limit ?";
    public List<LangYaBangWarReportData> getLangYaBangWarReportData(int activityId, long userId,int limit) {
        List<LangYaBangWarReportData> reportDataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getLangYaBangWarReportDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            call.setLong(i++, limit);
            rs = call.executeQuery();
            while (rs.next()) {
                LangYaBangWarReportData parse = parse(rs);
                reportDataList.add(parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return reportDataList;
    }

    public static String updateLangYaBangWarReportDataSql = "update `t_u_lang_ya_war_report` set otherUserId=?,operateType=?,operateTime=?,goodReward=?,coinReward=?,scoreChange=?,costCoin=?,otherData=?,time=? where activityId=?";
    @Override
    protected boolean updateData(LangYaBangWarReportData langYaBangWarReportData) {
        return true;
    }

    public static String insertLangYaBangWarReportDataSql = "insert into `t_u_lang_ya_war_report`(activityId,userId,otherUserId,operateType,operateTime,goodReward,coinReward,scoreChange,costCoin,otherData,time,selfScoreChange) values (?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(LangYaBangWarReportData langYaBangWarReportData) {
        langYaBangWarReportData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangWarReportData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangWarReportData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangWarReportData.getOtherUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangWarReportData.getOperateType()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangWarReportData.getOperateTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, langYaBangWarReportData.getGoodReward()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangWarReportData.getCoinReward()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangWarReportData.getScoreChange()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangWarReportData.getCostCoin()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaBangWarReportData.getOtherData())));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangWarReportData.getTime()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangWarReportData.getSelfScoreChange()));
        if (getDb().executeNoneQuery(insertLangYaBangWarReportDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected LangYaBangWarReportData parseData(ResultSet rs) throws SQLException {
        LangYaBangWarReportData langYaBangWarReportData = new LangYaBangWarReportData();
        langYaBangWarReportData.setActivityId(rs.getInt("activityId"));
        langYaBangWarReportData.setUserId(rs.getLong("userId"));
        langYaBangWarReportData.setOtherUserId(rs.getLong("otherUserId"));
        langYaBangWarReportData.setOperateType(rs.getInt("operateType"));
        langYaBangWarReportData.setOperateTime(rs.getInt("operateTime"));
        langYaBangWarReportData.setGoodReward(rs.getString("goodReward"));
        langYaBangWarReportData.setCoinReward(rs.getLong("coinReward"));
        langYaBangWarReportData.setScoreChange(rs.getLong("scoreChange"));
        langYaBangWarReportData.setCostCoin(rs.getLong("costCoin"));
        langYaBangWarReportData.setOtherData(JSON.parseObject(rs.getString("otherData"), LangYaUserBriefData.class));
        langYaBangWarReportData.setTime(rs.getLong("time"));
        langYaBangWarReportData.setSelfScoreChange(rs.getLong("selfScoreChange"));

        return langYaBangWarReportData;
    }


}
